package inits

import (
	"github.com/sirupsen/logrus"
	"grpc/user/global"
	"grpc/user/model"
	"grpc/user/utils"
	"os"
)

func PgsqlDBConnect() {
	pgsql := &global.Config.Pgsql
	db, err := pgsql.PgsqlDb()
	if err != nil {
		global.Logger.Fatal(err)
	}
	global.PgSqlDB = db

}

func RedisDBConnect() {
	r := &global.Config.Redis
	db, err := r.RedisDb()
	if err != nil {
		global.Logger.Fatal(err)
	}
	global.RedisDB = db
}

func DBMigrate() {
	err := global.PgSqlDB.AutoMigrate(
		&model.SysUser{},
	)
	if err != nil {
		global.Logger.Fatal(err)
	}
	global.Logger.Info("register table success")
}

func InitLogger() {
	logger := logrus.New()
	logger.AddHook(utils.NewContextHook())
	logger.SetFormatter(&logrus.JSONFormatter{})
	logger.SetOutput(os.Stdout)
	switch global.Config.Logger.Level {
	case "debug":
		logger.SetLevel(logrus.DebugLevel)
	case "info":
		logger.SetLevel(logrus.InfoLevel)
	case "error":
		logger.SetLevel(logrus.ErrorLevel)
	default:
		logger.SetLevel(logrus.WarnLevel)
	}
	global.Logger = logger
}
